package com.itheima.mobilesafe74.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.itheima.mobilesafe74.R;
import com.itheima.mobilesafe74.db.dao.BlackNumberDao;
import com.itheima.mobilesafe74.db.domain.BlackNumberInfo;
import com.itheima.mobilesafe74.utils.ToastUtil;

import java.util.List;

//listView的优化
//1、复用convertView
//2、对findViewById次数的优化，使用viewHolder
//3、价格ViewHolder定义成静态的，不会去创建多个对象
//4,listView如果有多个条目的时候,可以做分页算法,每一次加载20条,逆序返回
public class BlackNumberActivity extends Activity {
    private Button bt_add;
    private ListView lv_blacknumber;
    private BlackNumberDao mDao;
    private List<BlackNumberInfo> mblackNumberList;
    private MyAdapter mAdapter;
    private int mode=1;
    private boolean mIsLoad=false;
    private int mCount;
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (mAdapter==null){
                //4、告知lisiview可以去设置数据适配器
                mAdapter=new MyAdapter();
                lv_blacknumber.setAdapter(mAdapter);
            }else {
                mAdapter.notifyDataSetChanged();
            }
        }
    };

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mblackNumberList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            /*View view=null;
            //复用convertView是优化的方法
            if (convertView==null){
                view=View.inflate(getApplicationContext(),R.layout.listview_blacknumber_item,null);
            }else{
                view=convertView;
            }*/

            //复用ViewHolder步骤一
            ViewHolder holder=null;

            if (convertView==null){
                convertView=View.inflate(getApplicationContext(),R.layout.listview_blacknumber_item,null);
                //将findViewById过程封装到convertView==null的情景下去做执行
                //2、减少findViewById的次数
                //复用ViewHolder步骤三
                holder=new ViewHolder();
                //复用ViewHolder步骤四
                holder.tv_phone=(TextView)convertView.findViewById(R.id.tv_phone);
                holder.tv_mode=(TextView)convertView.findViewById(R.id.tv_mode);
                holder.iv_delete= (ImageView) convertView.findViewById(R.id.iv_delete);

                //复用ViewHolder步骤五
                convertView.setTag(holder);
            }else {
                holder= (ViewHolder) convertView.getTag();
            }


            holder.iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //1、数据库删除
                    mDao.delete(mblackNumberList.get(position).phone);
                    //2、集合中的删除，通知数据适配器刷新
                    mblackNumberList.remove(position);
                    if (mAdapter!=null){
                        mAdapter.notifyDataSetChanged();
                    }
                }
            });

            holder.tv_phone.setText(mblackNumberList.get(position).phone);
            //tv_mode.setText(mblackNumberList.get(position).mode);
            int mode=Integer.parseInt(mblackNumberList.get(position).mode);
            switch (mode){
                case 1:
                    holder.tv_mode.setText("拦截短信");
                    break;
                case 2:
                    holder.tv_mode.setText("拦截电话");
                    break;
                case 3:
                    holder.tv_mode.setText("拦截所有");
                    break;
            }
            return convertView;
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blacknumber);

        initUI();

        initData();
    }

    private void initData() {
        //获取数据库中所有电话号码
        new Thread(){
            @Override
            public void run() {
                //1、获取操作黑名单数据库的对象
                mDao= BlackNumberDao.getInstance(getApplicationContext());
                //2、查询部分数据
                mblackNumberList=mDao.find(0);
                mCount=mDao.getCount();
                //3、通过消息机制，告知主线程可以去使用包含数据的集合
                mHandler.sendEmptyMessage(0);

            }
        }.start();
    }
    /*private void loadDate(int index) {

    }*/

    private void initUI() {
        bt_add= (Button) findViewById(R.id.bt_add);
        lv_blacknumber= (ListView) findViewById(R.id.lv_blacknumber);

        bt_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });

        lv_blacknumber.setOnScrollListener(new AbsListView.OnScrollListener() {
            //滚动过程中，状态发生改变调用的方法（）
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                //AbsListView.OnScrollListener.SCROLL_STATE_FLING 飞速滚动
                //AbsListView.OnScrollListener.SCROLL_STATE_IDLE  空闲状态
                //AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL  拿手触摸着去滚动状态
                if (mblackNumberList!=null){
                //条件一：滚动到停止状态
                //条件二：最后一个条目可见（最后一个条目的索引值>=数据适配器中结合的总条数个数-1）
                    if (scrollState== AbsListView.OnScrollListener.SCROLL_STATE_IDLE
                            && lv_blacknumber.getLastVisiblePosition()>=mblackNumberList.size()-1
                            && !mIsLoad){
                        //mIsLoad 防止重复加载的变量
                        //如果当前正在加载mIsLoad就会为true，本次加载完毕后，再将mIsLoad改为false
                        //如果下一次加载需要去做执行的时候，会判断上诉的mIsLoad变量，是否为false，如果为true
                        //就需要等待上一次加载完成，将其值改为false后再去加载

                        //如果条目的总数大于集合大小时，才可以继续加载
                        if (mCount>mblackNumberList.size()){
                            //加载下一页数据
                            new Thread(){
                                @Override
                                public void run() {
                                    //1、获取操作黑名单数据库的对象
                                    mDao= BlackNumberDao.getInstance(getApplicationContext());
                                    //2、查询部分数据
                                    List<BlackNumberInfo> moreData=mDao.find(mblackNumberList.size());
                                    //3、添加下一页数据的过程
                                    mblackNumberList.addAll(moreData);
                                    //4、通知数据适配器刷新
                                    mHandler.sendEmptyMessage(0);
                                }
                            }.start();
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }

    private void showDialog() {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        final AlertDialog dialog=builder.create();
        View view=View.inflate(getApplicationContext(),R.layout.dialog_add_blacknumber,null);
        dialog.setView(view,0,0,0,0);

        final EditText et_phone= (EditText) view.findViewById(R.id.et_phone);
        RadioGroup rg_group= (RadioGroup) view.findViewById(R.id.rg_group);

        Button bt_submit= (Button) view.findViewById(R.id.bt_submit);
        Button bt_cancel= (Button) view.findViewById(R.id.bt_cancel);
        //监听其选中条目的切换过程
        rg_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_sms:
                        //拦截短信
                        mode=1;
                        break;
                    case R.id.rb_phone:
                        //拦截电话
                        mode=2;
                        break;
                    case R.id.rb_all:
                        //拦截所有
                        mode=3;
                        break;
                }
            }
        });

        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1、获取输入框中的电话号码
                String phone=et_phone.getText().toString();
                if (!TextUtils.isEmpty(phone)){
                    //2、数据库插入当前输入的拦截电话号码
                    mDao.insert(phone,mode+"");
                    //3、让数据库与集合保持同步（1：数据库中数据重新读一遍2：手动向集合中添加一个对象（插入数据构建的对象））
                    BlackNumberInfo blackNumberInfo=new BlackNumberInfo();
                    blackNumberInfo.phone=phone;
                    blackNumberInfo.mode=mode+"";

                    //4、将对象插入到集合的最顶部
                    mblackNumberList.add(0,blackNumberInfo);
                    //5、通知数据适配器刷新（数据适配器中的数据有改变）
                    if (mAdapter!=null){
                        mAdapter.notifyDataSetChanged();
                    }
                    //6、隐藏对话框
                    dialog.dismiss();
                }else {
                    ToastUtil.show(getApplicationContext(),"请输入拦截号码");
                }
            }
        });

        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
    }

    //复用ViewHolder步骤二
    static class ViewHolder {
        TextView tv_phone;
        TextView tv_mode;
        ImageView iv_delete;
    }
}
